<?php
namespace app\admin\controller;
use think\Session;
use think\Loader;
use app\model\Order;

class OrderController extends AuthController
{
    protected $order;

	protected function _initialize(){
        parent::_initialize();
		$this->order = new Order();
	}

    /**
     * 订单列表
     * @return mixed
     */
	public function index(){

        $time['startTime'] = $this->request->has('starttime') ? $this->request->param('starttime') : null;
        $time['endTime'] = $this->request->has('endtime') ? $this->request->param('endtime') : null;
        $status = $this->request->param('status');
        $order_num = $this->request->param('order_num');

        $status = ($status != null)||($status=='0') ? $status : 'all'; //防止状态0被过滤

        $startTime = $time['startTime'] ? strtotime($time['startTime']) : 0;
        $endTime = $time['endTime'] ? strtotime($time['endTime']) + 86400 : time();

        if(!is_null($order_num)&&($order_num != '')){
            $order = $this->order->where('order_num',$order_num);
        }
        else{
            $order = $this->order->order('create_time desc')
                ->where('create_time','between time',[$startTime,$endTime]);

            if($status != 'all'){
                $this->order->where('status',$status);
            }
        }
        $result = $order->paginate(20,false,[
            'query' => $this->request->param(),
        ]);

        $this->assign('time',$time);
        $this->assign('list',$result);
        return $this->fetch();
	}

    public function add(){
        if($this->request->isPost()){

            $order_data = $this->request->post();
            $order_data['admin_id'] = Session::get('admin_id');

            $rand = builderRand(8);
            $order_data['order_num'] = date('Ymd').$rand;

            //计算工资
            $salary = getSalary($order_data['type'],$order_data['money']);
            $order_data['salary_ratio'] = $salary['ratio'];
            $order_data['salary'] = $salary['salary'];

            //项目名称
            $item = findItemById($order_data['item']);
            $order_data['item_name'] = $item['name'];

            //验证
            $validate = Loader::validate('Order');
            $validate->check($order_data) ?  : $this->error($validate->getError());

            //更新魅力,统一单位1元=1
            if(($order_data['type'] == 1)&&($order_data['member_id'] != 0)) {
                $charm = intval($order_data['money']);
                incCharm($order_data['member_id'],$charm);
            }

            $this->order->save($order_data) ? $this->success('创建成功','admin/Order/index'):$this->error($this->order->getError());
        }
        else{
            return $this->fetch();
        }
    }

    public function edit(){
        if($this->request->isPost()){

            if(!$this->request->has('id')){
                $this->error('编辑失败');  
            }

            $order_data = $this->request->post();
            $this_order = Order::get(['id'=>$order_data['id']]);
            //公众号
            if($this_order['origin'] == 0){
                $order['admin_id'] = Session::get('admin_id');
                $order['member_id'] = $order_data['member_id'];
                $order['member_name'] = $order_data['member_name'];
                $order['remark'] = $order_data['remark'];
                $order['status'] = $order_data['status'];
                $this->order->save($order,['id' => $order_data['id']])? $this->success('编辑成功'):$this->error($this->order->getError());
            }
            //普通
            else{
                //计算工资
                $salary = getSalary($order_data['type'],$order_data['money']);
                $order_data['salary_ratio'] = $salary['ratio'];
                $order_data['salary'] = $salary['salary'];
                $order_data['admin_id'] = Session::get('admin_id');
                //项目名称
                $item = findItemById($order_data['item']);
                $order_data['item_name'] = $item['name'];
                //验证
                $validate = Loader::validate('Order');
                $validate->check($order_data) ?  : $this->error($validate->getError());
                $this->order->save($order_data,['id' => $order_data['id']])? $this->success('编辑成功'):$this->error($this->order->getError());
            }
        }
        else{
            $id = $this->request->param('id');
            $id ? : $this->error('编辑失败');

            $result = $this->order->get($id);


            $this->assign('order',$result);
            return $this->fetch();
        }
    }

    /**
     * 订单删除
     * @return array
     */
    public function del(){
        if($this->request->isPost()) {
            $id = $this->request->post('id');
            if (!$this->request->has('id')) {
                $this->error('删除失败');
            }
            return Order::destroy(['id' => $id]) ? msgReturn(true,'success',null):msgReturn(false,$this->order->getError(),null);
        }
    }

    /**
     * 订单状态编辑
     * @return array
     */
    public function editStatus(){
        if($this->request->isPost()) {
            $data = [
                'id' => $this->request->post('id'),
                'status' => $this->request->post('status'),
            ];
            if (!$this->request->has('id')) {
                return msgReturn(false,'编辑失败',null);
            }
            $order = Order::get($data['id']);
            return $order->save(['status' => $data['status']]) ? msgReturn(true,'编辑成功','success') : msgReturn(false,$this->order->getError(),null);
        }
    }

    /**
     * 订单评价
     */
    public function review(){

    }

}